//
//  LGPhotoReportController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/11/8.
//

#import "LGPhotoReportController.h"
#import "LGAddOutpatientRecordController.h"
//查看详情
#import "LGPhotoReportDetailController.h"
#import "LGPhotoReportCell.h"
@interface LGPhotoReportController ()<UITableViewDataSource,UITableViewDelegate,LGPhotoReportDelegate>
/**   */
@property (nonatomic , strong)UIView *topLine;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UIButton *addButton;
/**   */
@property (nonatomic , strong)NSArray *dataArray;


@end

@implementation LGPhotoReportController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    [self.view addSubview:self.topLine];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.addButton];
    [self emptyViewWithSuperView:self.view frame:self.tableView.frame];
    self.emptyView.backgroundColor = [UIColor clearColor];
}
-(void)viewWillAppearAction{
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/PhysicalRecord/reportFileList" parameters:@{@"patient_id":self.patientid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if (code == 200) {
            self.dataArray = returnData;
            [self.tableView reloadData];
        }
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无数据" offset:0];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无数据" offset:0];
        }
    }];
}

//新增病历
-(void)addButtonAction{
    LGAddOutpatientRecordController *controller = [[LGAddOutpatientRecordController alloc]init];
    controller.patientid = self.patientid;
    [self.navigationController pushViewController:controller animated:YES];
    __weak typeof(self) weakSelf = self;
    controller.addReportSuccess = ^{
        [weakSelf viewWillAppearAction];
    };
}

//查看全部
-(void)checkPhotoReportDetail:(NSDictionary *)dataDic{
    LGPhotoReportDetailController *controller = [[LGPhotoReportDetailController alloc]init];
    controller.dataDic = dataDic;
    [self.navigationController pushViewController:controller animated:YES];
}

//报告详情
-(void)checkFileDetail:(NSDictionary *)dataDic{
    LGGetWebViewController *controller = [[LGGetWebViewController alloc]init];
    controller.urlStr = LGNSString(dataDic[@"file_url"]);
    [self.navigationController pushViewController:controller animated:YES];
}

//-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//    return self.dataArray.count;
//}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGPhotoReportCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dataDic = self.dataArray[indexPath.row];
    cell.delegate = self;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    LGMainInspectItemController *controller = [[LGMainInspectItemController alloc]init];
//    controller.fullowDate = LGNSString(self.dataArray[indexPath.section]);
//    controller.patientid = self.patientid;
//    [self.navigationController pushViewController:controller animated:YES];
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
}

#pragma mark
#pragma mark ====> 创建控件
-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.tableView.frame = CGRectMake(0, 0, Screen_W, self.view.bounds.size.height-viewPix(65));
    self.emptyView.frame = CGRectMake(0, 0, Screen_W, self.view.bounds.size.height-viewPix(65));
    self.addButton.frame = CGRectMake(viewPix(16), kMaxY(self.tableView.frame)+viewPix(5), Screen_W-viewPix(32), viewPix(46));
}

-(UIView *)topLine{
    if(!_topLine){
        _topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, 1)];
        _topLine.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
    }
    return _topLine;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight-viewPix(44)) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGPhotoReportCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
    }
    return _tableView;
}

-(UIButton *)addButton{
    if(!_addButton){
        _addButton = [UIButton buttonWithTitle:@"上传报告" topMargin:kMaxY(self.tableView.frame)+viewPix(5)];
        [_addButton addTarget:self action:@selector(addButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addButton;
}

@end
